import { createRouter, createWebHashHistory } from "vue-router";
// import Layout from '@/views/Layout.vue'
// 路由懒加载形式导入Layout
const Layout = () => import('@/views/Layout.vue')
const Home = () => import('@/views/home/index.vue')
const Category = () => import('@/views/category/index.vue')
const Search = () => import('@/views/search/index.vue')
const Goods = () => import('@/views/goods/index.vue')
const Login = () => import('@/views/login/index')
// 定义路由规则:什么路径显示什么组件
const routes = [
  {
    path: "/",
    name: "home",
    component: Layout,
    children: [
      { path: '/', component: Home },
      { path: "/category/:id", component: Category },
      { path: "/search", component: Search },
      { path: "/product/:id", component: Goods }

    ]
  },
  { path: '/login', component: Login },
  {
    path: "/about",
    name: "about",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 前进后退的时候，回到页面原理滚动的位置（还原浏览器原生效果）
    if (savedPosition) {
      return savedPosition;
    } else {
      // 没有浏览过的到页面顶部
      return { top: 0 };
    }
  },
});

export default router;
